package configlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/recharge"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetRechargeChannelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetRechargeChannelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRechargeChannelLogic {
	return &GetRechargeChannelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetRechargeChannelLogic) GetRechargeChannel(in *rpc.RechargeChannelRequest) (*rpc.RechargeChannelResponse, error) {
	var rechargeChannels []*recharge.RechargeChannel
	var rechargeChannelInfoList []*rpc.RechargeChannelInfo

	// 查询所有用户银行信息
	query := l.svcCtx.Db
	if in.RechargeId != 0 {
		query = query.Where("merchant_id = ?", in.RechargeId)
	}
	if in.Name != "" {
		query = query.Where("name LIKE ?", "%"+in.Name+"%")
	}
	if in.Status != 0 {
		query = query.Where("status = ?", in.Status)
	}
	if in.Type != 0 {
		query = query.Where("type = ?", in.Type)
	}

	tx := query.Find(&rechargeChannels)
	if tx.Error != nil {
		return nil, tx.Error
	}

	_ = copier.Copy(&rechargeChannelInfoList, rechargeChannels)

	return &rpc.RechargeChannelResponse{
		RechargeChannelInfoList: rechargeChannelInfoList,
	}, nil
}
